package com.zxl.live.screen.ui.receiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.zxl.live.tools.analytics.AnalyticsHelper;
import com.zxl.live.tools.analytics.Event;
import com.zxl.live.tools.analytics.Params;
import com.zxl.live.tools.tools.LogUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 * Created by Administrator on 2018/2/6.
 */

public class GaTrackerReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (null != intent) {
            String referrer = intent.getStringExtra("referrer");
            String utmSource = "";
            ReferrerManager refererManager = ReferrerManager.getInstance(context);
            if (!TextUtils.isEmpty(referrer) && refererManager.checkFirstReceiver()) {
                try {
                    String refer1 = URLDecoder.decode(referrer, "utf-8");
                    utmSource = refererManager.getUtmSource(refer1);
                    refererManager.saveFirstReceiver();
                    refererManager.saveReferrer(refer1);
                    LogUtils.i("referrer = " + refer1 + ",utmSource=" + utmSource);
                } catch (UnsupportedEncodingException e1) {
                    e1.printStackTrace();
                    utmSource = refererManager.getUtmSource(referrer);
                    LogUtils.i("referrer = " + referrer + ",utmSource=" + utmSource);
                    refererManager.saveFirstReceiver();
                    refererManager.saveReferrer(referrer);
                } finally {
                    AnalyticsHelper.logEvent(new AnalyticsHelper.LogEvent(Event.NEW_USE).addParam(Params.UTM_SOURCE, utmSource));
                }
            }
        }
    }
}
